package org.example.view;

import org.example.StudentApplication;
import org.example.dao.UsersDao;
import org.example.pojo.User;

import org.example.service.AdminService;
import org.example.utils.QQsendUtils;

import java.sql.SQLException;
import java.util.Scanner;

public class UserView {
    private Scanner scanner = new Scanner(System.in);
    private AdminService adminService = new AdminService();

    public User welcome() throws Exception {
        while (true) {
            System.out.println("---------欢迎来到博客-----------");
            System.out.println("---------请选择一个选项：--------");
            System.out.println("|          1. 游客访问                |");
            System.out.println("|          2. 登录                   |");
            System.out.println("|          3. 注册                   |");
            System.out.println("|          4. 忘记密码                |");
            System.out.println("|          0. 退出                   |");
            System.out.println("-------------------------------------");

            String input = scanner.nextLine();
            int choice = -1;

            try {
                choice = Integer.parseInt(input);
            } catch (NumberFormatException e) {
                System.out.println("无效的输入，请输入一个数字！");
                continue;
            }

            switch (choice) {
                case 1: {
                    try {
                        new MeneView().showMainMenu(new Scanner(System.in),0,"游客");
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                    return null;
                }
                case 2: {
                    System.out.println("|          1. 用户                   |");
                    System.out.println("|          2. 管理员                 |");
                    int i = scanner.nextInt();
                    scanner.nextLine();
                    switch (i) {
                        case 1: {
                            User login = login();
                            if (login != null) {
                                new StudentApplication();
                                return login;
                            }
                            break;
                        }
                        case 2: {  //注册
                            showAdminLoginMenu();
                            break;
                        }
                        default: {
                            System.out.println("无效的选择，请重新输入！");
                            break;
                        }
                    }
                    break;
                }
                case 3: {
                    register();
                    break;
                }
                case 4: {  // 忘记 密码
                    System.out.println("请输入用户名");
                    String username = scanner.next();
                    System.out.println("请输入邮箱");
                    String email = scanner.next();
                    String password = new UsersDao().getPassword(username, email);
                    if (password!=null) {
                        System.out.println(username+"的密码为："+password);
                    } else {
                        System.out.println("用户名或邮箱错误");
                    }
                }
                case 0: {
                    System.out.println("退出系统。");
                    return null;
                }
                default: {
                    System.out.println("无效的选择，请重新输入！");
                    break;
                }
            }
        }
    }

    public User login() {
        System.out.println("用户名: ");
        String username = scanner.next();
        System.out.println("密码: ");
        String password = scanner.next();

        try {
            User login = new UsersDao().login(username, password);
            if (login != null) {
                System.out.println("登录成功");
                return login;
            } else {
                System.out.println("登录失败,用户名或密码错误");
            }
        } catch (SQLException e) {
            System.out.println("登录失败: " + e.getMessage());
        }
        return null;
    }

    private void showAdminLoginMenu() {
        System.out.println("\n管理员登录菜单:");
        System.out.println("1. 管理员登录");
        System.out.println("2. 管理员注册");
        System.out.println("0. 返回");
        System.out.print("请输入您的选择：");
        int choice = scanner.nextInt();
        scanner.nextLine(); // Consume the newline left-over

        switch (choice) {
            case 1:
                adminLogin();
                break;
            case 2:
                adminRegister();
                break;
            case 0:
                return;
            default:
                System.out.println("无效的选择，请重新输入！");
                showAdminLoginMenu();
                break;
        }
    }

    // 管理员登录的方法
    private boolean adminLogin() {
        System.out.print("请输入管理员用户名: ");
        String username = scanner.nextLine();
        System.out.print("请输入管理员密码: ");
        String password = scanner.nextLine();

        if (adminService.login(username, password) != null) {
            System.out.println("管理员登录成功！");
            new ManageView().showAdminMainMenu();
        } else {
            System.out.println("登录失败，请检查用户名和密码！");
            showAdminLoginMenu();
        }
        return false;
    }

    // 管理员注册的方法
    private void adminRegister() {
        System.out.print("请输入管理员用户名: ");
        String username = scanner.nextLine();
        System.out.print("请输入管理员密码: ");
        String password = scanner.nextLine();

        if (adminService.register(username, password)) {
            System.out.println("管理员注册成功！");
        } else {
            System.out.println("注册失败，请重试！");
        }
        showAdminLoginMenu();
    }

    public void register() {
        System.out.println("注册的用户名: ");
        String username = scanner.next();
        System.out.println("密码: ");
        String password = scanner.next();
        System.out.println("QQ邮箱: ");
        String pattern="[1-9][0-9]+@qq.com";
        String email = scanner.next();
        boolean b=true;
        while (b){
            if (email.matches(pattern)){
                b=false;
            }else {
                System.out.println("邮箱格式错误，请重新输入");
                email = scanner.next();
            }
        }
        System.out.println("验证码正在发往邮箱，请耐心等待!");
        String s = new QQsendUtils().sendEmail(email);
        System.out.println("请输入验证码：");
        if(s.equals(scanner.next())){
            try {
                // 清除缓冲区
                scanner.nextLine();
                new UsersDao().register(username, password, email);

            } catch (SQLException e) {
                System.out.println("注册失败: " + e.getMessage());
            }
        }else {
            System.out.println("验证码错误");
        }

    }
}
